package com.srd.phone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.shengrongdai.srd.R;

public class WebViewActivity extends AppCompatActivity {

    private WebView webView;
    private ImageView iv_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        initView();
        initData();
    }

    public void initView() {

        webView = (WebView) findViewById(R.id.webview);

        configWebView();

        iv_close = (ImageView)findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public void initData() {

        Intent intent = this.getIntent();
        final String urlValue = intent.getStringExtra("urlValue");
        final String titleName = intent.getStringExtra("titleName");
//        if(TextUtils.isEmpty(titleName))
//            tv_common_title.setText("");
//        else
//            tv_common_title.setText(titleName);

        webView.loadUrl(urlValue);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(urlValue);
                return true;
            }
        });

//        webView.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                super.onProgressChanged(view, newProgress);
//                if (newProgress == 0) {
//                    showWaitingDialog();
//                } else if (newProgress == 100) {
//                    cancelWaitingDialog();
//                }
//            }
//        });
    }

    private void configWebView(){
        WebSettings ws = webView.getSettings();
        ws.setBuiltInZoomControls(true);
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        ws.setUseWideViewPort(true);
        ws.setLoadWithOverviewMode(true);
        ws.setSavePassword(true);
        ws.setSaveFormData(true);
        ws.setJavaScriptEnabled(true);
        ws.setGeolocationEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        ws.setGeolocationDatabasePath(appCachePath);
        ws.setDomStorageEnabled(true);
        webView.requestFocus();
    }
}
